// pages/trucks/searchCar/searchCar.js
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast';
// var cityData = require('../../../utils/citydata.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    load: '请选择装货地',
    province_zid: '北京市', //装货省
    city_zid: '北京市辖区', //装货市
    area_zid: '东城区', //装货区
    loadValue: '', //装货地详细地址
    upLoad: '请选择卸货地',
    province_xid: '北京市', //卸货省
    city_xid: '北京市辖区', //卸货市
    area_xid: '东城区', //卸货区
    upLoadValue: '', //卸货地详细地址
    showLoad: false, //装货地弹窗
    seleteColor: '', //选中改变颜色
    seletesColor: '',
    cargo: false, //货主分类弹窗
    sortCon: '', //货主分类
    cargoSort: '', //自定义货物
    price: '', //运费价格
    infor: '', //发货人信息
    name: '', //发货人姓名
    phone: '', //发货人手机号
    spare: '', //发货人备用手机号
    model: '', //车型
    remark: '', //备注
    getCar: false, //车型弹窗
    cargoList: [], //货主分类列表
    sortList: [], //接收分类列表
    carlengthList: [], //车长
    carModelList: [], //车型
    seleteCarlong: null, //选中车长
    seleteCarModel: null, //选中车型
    noneModel: false, //自定义车型获得焦点
    customModel: '',//自定义车型内容
    type_id: '', //货主类型id
    car_id: '', //车型id
    conductor_id: '', //车长id

    // citysData: cityData.citysData,
    citysData: [],
    provinces: [],
    citys: [],
    areas: [],
    value: [0,0,0],
    type: ''//判断点击哪个省市区
  },
  initData: function(){
    var provinces = [];
    var citys = [];
    var areas = [];
    this.data.citysData.forEach(function(province,i){
        provinces.push(province.name);
        if(i===0){
            citys.push(province.citys[i].name);
            areas = province.citys[i].areas;
        }
    });

    this.setData({
        provinces: provinces,
        citys: citys,
        areas: areas
    });
  },

  bindChange: function(e){
    console.log(e)
    var citysData = this.data.citysData;
    var value = this.data.value;
    var current_value = e.detail.value;
    var citys = [];

    var provinceObj = {};
    var cityObj = {};
    provinceObj = citysData[current_value[0]];

    if (value[0] !== current_value[0]) {
        // 滑动省份
        provinceObj.citys.forEach(function(v){
            citys.push(v.name);
        });
        this.setData({
            citys: citys
        });

        cityObj = provinceObj.citys[0];
        this.setData({
            areas: cityObj.areas,
            value: [current_value[0],0,0]
        });

    }else if(value[0] === current_value[0] && value[1] !== current_value[1]){
        // 滑动城市
        if(current_value[1]>=provinceObj.citys.length){
            // 数据不存在 跳过
            return;
        }
        cityObj = provinceObj.citys[current_value[1]];
        this.setData({
            areas: cityObj.areas,
            value: [current_value[0],current_value[1],0]
        });
    }else{
        // 滑动区县
        cityObj = provinceObj.citys[current_value[1]];
        this.setData({
            value: current_value
        });
    }
    if(this.data.type == 0) {
      this.setData({
        province_zid: provinceObj.name,
        city_zid: cityObj.name,
        area_zid: cityObj.areas[this.data.value[2]]
      });
    }else{
      this.setData({
        province_xid: provinceObj.name,
        city_xid: cityObj.name,
        area_xid: cityObj.areas[this.data.value[2]]
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getType() //获取货物类型
    this.getAddress() //获取地区
    // this.initData() //省市区
    this.getlongList() //获取车长
    this.getModelList() //获取车型
  },
    /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.getStorageSync('inforStatus') == 1) {
      this.setData({
        name: wx.getStorageSync('name'),
        phone: wx.getStorageSync('phone'),
      })
      this.setData({
        infor: this.data.name +'/'+ this.data.phone
      })
    }
  },
  //发货人
  shipper() {
    wx.navigateTo({
      url: '/pages/trucks/shipper/shipper'
    })
  },
  //装货详细地址
  loadDetail(e) {
    this.setData({
      loadValue: e.detail.value
    })
  },
  //卸货详细地址
  upLoadDetail(e) {
    this.setData({
      upLoadValue: e.detail.value
    })
  },
  //蒙层关闭
  onClose() {
    this.setData({ 
      showLoad: false,
      cargo: false,
      getCar: false
    });
  },
  //显示装货地
  showLoad(e) {
    console.log(e)
    this.setData({
      type: e.currentTarget.dataset.type,
      showLoad: true
    })
  },
  //取消装货地
  loadCancel() {
    this.setData({
      showLoad: false
    })
  },
  //装货地确定按钮
  loadConfirm() {
    if(this.data.type == 0) {
      this.setData({
        showLoad: false,
        load: this.data.province_zid+' '+this.data.city_zid+' '+this.data.area_zid,
        seleteColor: '#333333'
      })
    }else{
      this.setData({
        showLoad: false,
        upLoad: this.data.province_xid+' '+this.data.city_xid+' '+this.data.area_xid,
        seletesColor: '#333333'
      })
    }
  },
  //货物数量
  getcargo(e) {
    this.setData({
      cargoSort: e.detail.value
    })
  },
  //货物数量失去焦点
  numBlur() {
    this.setData({
      cargoSort: this.data.cargoSort + '吨'
    })
  },
  //运费价格
  getPrice(e) {
    this.setData({
      price: e.detail.value
    })
  },
  //运费价格失去焦点
  priceBlur() {
    this.setData({
      price: this.data.price + '元/吨'
    })
  },
  //备注
  getModel(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  //货物分类弹窗
  sort() {
    this.setData({ 
      cargo: true
    })
  },
  //货物选择器滑动
  cargonChange(e) {
    console.log(e)
  },
  //货物取消按钮
  cargoCancel() {
    this.setData({ 
      cargo: false 
    })
  },
  //货物确定按钮
  cargoConfirm(e) {
    console.log(e)
    let arr = this.data.cargoList.filter(function(item){
      return item.name == e.detail.value;
    })
    this.setData({
      sortCon: e.detail.value,
      cargo: false,
      type_id: arr[0].id,
    })
    // if(this.data.sortCon == '散装') {
    //   wx.request({
    //     url: app.globalData.url + 'api/v1/goods/goodsresources/freight',
    //     method: 'GET',
    //     success: res => {
    //       console.log(res)
    //       if(res.data.code == 200) {
    //         this.setData({
    //           price: res.data.data.data + '元/吨',
    //           priceDisabled: true
    //         })
    //       }
    //     },
    //     fail: err => {
    //       console.log(err)
    //     }
    //   })
    // }else{
    //   this.setData({
    //     price: '',
    //     priceDisabled: false
    //   })
    // }
  },
  //车型弹窗
  carModel() {
    this.setData({
      getCar: true
    })
  },
  //车型取消按钮
  cancel() {
    this.setData({
      getCar: false,
      seleteCarlong: null,
      seleteCarModel: null,
      noneModel: false,
      customModel: ''
    })
  },
  //切换车长
  changeLength(e) {
    console.log(e)
    this.setData({
      seleteCarlong: e.target.dataset.index,
      conductor_id: e.target.dataset.id
    })
  },
  //切换车型
  changeModel(e) {
    console.log(e)
    this.setData({
      seleteCarModel: e.target.dataset.index,
      car_id: e.target.dataset.id
    })
    console.log('车型id：'+this.data.car_id)
    if(this.data.noneModel == true){
      this.setData({
        noneModel: false,
        customModel: '' //自定义车型
      })
    }
  },
  //自定义车型获得焦点
  focusModel() {
    this.setData({
      seleteCarModel: null,
      noneModel: true,
      car_id: '' //车型id
    })
  },
  //输入自定义车型
  goModel(e) {
    this.setData({
      customModel: e.detail.value
    })
  },
  //车型确定按钮
  sure() {
    const customModel = this.data.customModel;
    if(this.data.seleteCarlong == null){
      wx.showToast({
        title: '请选择车长！',
        icon: 'none',
        mask: true
      })
    }else if(this.data.seleteCarModel == null && this.data.customModel == ''){
      wx.showToast({
        title: '请选择车型！',
        icon: 'none',
        mask: true
      })
    }else{
      if(this.data.noneModel == true){
        var value = this.data.carlengthList[this.data.seleteCarlong].conductor + '米/' + customModel
      }else{
        var value = this.data.carlengthList[this.data.seleteCarlong].conductor + '米/' + this.data.carModelList[this.data.seleteCarModel].name
      }
      this.setData({
        getCar: false,
        model: value
      })
    }
  },
  //获取地区
  getAddress() {
    wx.request({
      url: app.globalData.url + 'api/v1/news/region',
      method: 'GET',
      success: res => {
        console.log(res)
        this.setData({
          citysData: res.data.data
        })
        this.initData()
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //获取货物类型
  getType() {
    wx.request({
      url: app.globalData.url + 'api/v1/news/gettype',
      method: 'GET',
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          this.setData({
            cargoList: res.data.data
          })
          // 定义brr接收cargoList数组里的name
          var brr = this.data.cargoList.map(function(item){
              return item.name
          })
          this.setData({
            sortList: brr
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //获取车长
  getlongList() {
    wx.request({
      url: app.globalData.url + 'api/v1/news/conductor',
      method: 'GET',
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          this.setData({
            carlengthList: res.data.data
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //获取车型
  getModelList() {
    wx.request({
      url: app.globalData.url + 'api/v1/news/getcar',
      method: 'GET',
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          this.setData({
            carModelList: res.data.data
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //提交
  submit() {
    if(this.data.load == '请选择装货地'){
      wx.showToast({
        title: '请选择装货地！',
        icon: 'none',
        mask: true
      })
    // }else if(this.data.loadValue == ''){
    //   wx.showToast({
    //     title: '请填写装货地详细地址！',
    //     icon: 'none',
    //     mask: true
    //   })
    }else if(this.data.upLoad == '请选择卸货地'){
      wx.showToast({
        title: '请选择卸货地！',
        icon: 'none',
        mask: true
      })
    // }else if(this.data.upLoadValue == ''){
    //   wx.showToast({
    //     title: '请填写卸货地详细地址！',
    //     icon: 'none',
    //     mask: true
    //   })
    }else if(this.data.type_id == ''){
      wx.showToast({
        title: '请选择货物分类！',
        icon: 'none',
        mask: true
      })
    }else if(this.data.cargoSort == ''){
      wx.showToast({
        title: '请填写货物数量！',
        icon: 'none',
        mask: true
      })
    }else if(this.data.price == ''){
      wx.showToast({
        title: '请填写运费价格！',
        icon: 'none',
        mask: true
      })
    }else if(this.data.infor == ''){
      wx.showToast({
        title: '请填写发货人信息！',
        icon: 'none',
        mask: true
      })
    }else if(this.data.model == ''){
      wx.showToast({
        title: '请选择车型！',
        icon: 'none',
        mask: true
      })
    }else{
      wx.request({
        url: app.globalData.url + 'api/v1/goods/goodsresources/goods',
        method: 'POST',
        data: {
          goodser_id: wx.getStorageSync('id'), //货主id
          name: this.data.name, //姓名
          img: wx.getStorageSync('headImg'), //头像
          name: this.data.name, //姓名
          phone: this.data.phone, //手机号
          spare: this.data.spare, //备用手机号
          title: this.data.cargoSort, //自定义货物名称
          type_id: this.data.type_id, //货物类型
          car_id: this.data.car_id, //车型
          custom: this.data.customModel, //自定义车型
          conductor_id: this.data.conductor_id, //车长
          freight: this.data.price, //运费价格
          content: this.data.remark, //备注
          province_zid: this.data.province_zid, //装货省!
          city_zid: this.data.city_zid, //装货市！
          area_zid: this.data.area_zid, //装货区！
          zplace: this.data.loadValue, //装货区详细地址
          province_xid: this.data.province_xid, //卸货省！
          city_xid: this.data.city_xid, //卸货市
          area_xid: this.data.area_xid, //卸货区
          xplace:this.data.upLoadValue, //卸货地详细地址
        },
        success: res => {
          console.log(res)
          if(res.data.code == 200) {
            setTimeout(() => {
              wx.showToast({
                title: '提交成功',
                icon: 'none',
                mask: true
              })
            }, 300);
            wx.removeStorageSync('name')
            wx.removeStorageSync('phone')
            wx.removeStorageSync('phones')
            wx.removeStorageSync('inforStatus')
            wx.navigateBack()
          }
        },
        fail: err => {
          console.log(err)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})